package commands;

import java.util.Enumeration;

import nanoxml.XMLElement;
import utils.ITransmittable;
import client.GameSettings;

/**
 * This command represents a request to open a new game table.
 */
public class CommandNewTable implements ITransmittable
{
	private static final String ATTR_USER_NAME = "Username";
	private static final String ATTR_OWNER_IP = "OwnerIP";
	
	private String username = null; 
	private String ownerIP = null;
	private GameSettings gameSettings = null;

	public CommandNewTable(String username, String ownerIP, GameSettings gameSettings)
	{
		this.username = username;
		this.ownerIP = ownerIP;
		this.gameSettings = gameSettings;
	}
	
	@SuppressWarnings("unchecked")
	public CommandNewTable(XMLElement command)
	{
		this.username = command.getStringAttribute(ATTR_USER_NAME);
		this.ownerIP = command.getStringAttribute(ATTR_OWNER_IP);
		
		Enumeration<XMLElement> enumerationXMLElements = command.enumerateChildren();
		while (enumerationXMLElements.hasMoreElements())
		{
			XMLElement childMessage = enumerationXMLElements.nextElement();

			if (childMessage.getName().equals(Commands.GAME_SETTINGS))
			{
				gameSettings = new GameSettings(childMessage);
			}
		}
	}

	@Override
	public XMLElement toXML()
	{
		XMLElement message  = new XMLElement();
		message.setName(Commands.NEW_TABLE);
		message.setAttribute(ATTR_USER_NAME, username);
		message.setAttribute(ATTR_OWNER_IP, ownerIP);
		
		XMLElement elementGameSettings = gameSettings.toXML();
		message.addChild(elementGameSettings);

		return message;
	}
	
	public String getUsername()
	{
		return username;
	}
	public String getOwnerIP() {
		return ownerIP;
	}
	
	public GameSettings getGameSettings()
	{
		return gameSettings;
	}
}
